from ..log import log_gui

class BaseListItem:
  STATE_NORMAL = 0
  STATE_HIGHLIGHTED = 1

  ALIGN_LEFT = 0
  ALIGN_CENTER = 1

  # Return one of these from OnTap. If LATER is returned then it's the item's
  # responsibility to call self.list.ActionDone() at some point or the list will
  # be stuck with an item highlighted and not respond to input.
  ON_TAP_UNHIGHLIGHT_NOW = None
  ON_TAP_UNHIGHLIGHT_LATER = 1

  def __init__(self, height):
    self.height = height
    self.highlighted = False
    self.caches = {}
    self.align_horizontal = BaseListItem.ALIGN_LEFT
    self.list = None

  def Draw(self, screen, window, x, y, dest_pixmap):
    pass

  def GetState(self):
    if self.highlighted:
      return BaseListItem.STATE_HIGHLIGHTED
    else:
      return BaseListItem.STATE_NORMAL

  def SetHighlighted(self, value):
    if value == self.highlighted:
      return
    self.highlighted = value
    state = self.GetState()
    if state in self.caches:
      del self.caches[state]

  def OnTap(self):
    """Return True to unhighlight, False to leave highlighted."""
    log_gui("************** Item tapped.************** ")
    return BaseListItem.ON_TAP_UNHIGHLIGHT_NOW

  def OnAdd(self, list):
    self.list = list

  def LoadMenuOnTap(self, app, screen_name, entry):
    def OnScreenChangeComplete():
      self.list.ActionDone()
    def OnTap():
      app.SetCurrentScreen(screen_name, entry, OnScreenChangeComplete)
      return BaseListItem.ON_TAP_UNHIGHLIGHT_LATER
    return OnTap
